//
//  UIButton+Extension.swift
//  Library_y
//
//  Created by JC on 2023/6/6.
//

import Foundation
import UIKit

public enum UDButtonStyle{
    case normal   // 正常操作
    case cancel   // 取消操作
    case warning  // 警告操作
}

// MARK: - 属性
extension UIButton {
    
    /// 设置图片在右侧，文件在左侧
    public func setforceRightToLeft() {
        transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
        titleLabel?.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
        imageView?.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
    }
}

// MARK: - 布局
public extension UIButton {
    
    /// 激活标题和图像水平布局
    func activeHorizontalLayout() {
        imageView?.contentMode = .scaleAspectFit
    }
    
    /// 激活标题和图像水平布局
    ///
    /// - parameter spacing: 标题与图像的水平间隔
    func activeVerticalLayout(with spacing: CGFloat = 5) {
        
        guard let imageSize = imageView?.frame.size else { return }
        titleEdgeInsets = UIEdgeInsets(top: 0,
                                       left: -imageSize.width,
                                       bottom: -(imageSize.height + spacing),
                                       right: 0)
        guard let titleSize = titleLabel?.frame.size else { return }
        imageEdgeInsets = UIEdgeInsets(top: -(titleSize.height + spacing),
                                       left: 0,
                                       bottom: 0,
                                       right: -titleSize.width)
    }
}

extension UIButton {
    public func customButtomWith(style: UDButtonStyle = .normal) {
        self.layer.masksToBounds = true
        self.layer.cornerRadius = 12.0
        self.backgroundColor = UIColor.white
        self.setTitleColor(UIColor(hex: "#FF7200"), for: .disabled)
        switch style {
        case .normal:
            self.setTitleColor(Color.hex_0, for: .normal)
        case .cancel:
            self.setTitleColor(Color.hex_6, for: .normal)
        case .warning:
            self.setTitleColor(Color.hex_6, for: .normal)
        }
    }
}

/// 扩大UIButton点击热区
fileprivate var rectNameKey:(Character?,Character?,Character?,Character?)

extension UIButton {
    func setEnlargeEdgeWith(top:CGFloat,right:CGFloat,bottom:CGFloat,left:CGFloat) {
        objc_setAssociatedObject(self, &rectNameKey.0, top, .OBJC_ASSOCIATION_COPY_NONATOMIC)
        objc_setAssociatedObject(self, &rectNameKey.1, right, .OBJC_ASSOCIATION_COPY_NONATOMIC)
        objc_setAssociatedObject(self, &rectNameKey.2, bottom, .OBJC_ASSOCIATION_COPY_NONATOMIC)
        objc_setAssociatedObject(self, &rectNameKey.3, left, .OBJC_ASSOCIATION_COPY_NONATOMIC)
    }
    
    open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        
        if let topEdge = objc_getAssociatedObject(self, &rectNameKey.0) as? CGFloat,
            let rightEdge = objc_getAssociatedObject(self, &rectNameKey.1) as? CGFloat,
            let bottomEdge = objc_getAssociatedObject(self, &rectNameKey.2) as? CGFloat,
            let leftEdge = objc_getAssociatedObject(self, &rectNameKey.3) as? CGFloat
        {
            return CGRect(x: bounds.origin.x - leftEdge, y: bounds.origin.y - topEdge, width: bounds.width + leftEdge + rightEdge, height: bounds.height + topEdge + bottomEdge).contains(point) ? self : nil
        }
        return super.hitTest(point, with: event)
    }
}
